package cn.fanyamin.util;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;

public class MathUtils {
    /*
     * return the max integer value of a divide b
     * 
     * @para int a, int b
     * 
     * @return Integer as ceil(a/b)
     */
    public static Integer divideAndCeil(int a, int b) {
        Double ret = null;
        double aa = a;
        double bb = b;
        if (bb != 0)
            ret = Math.ceil(aa / bb);
        if (ret != null)
            return ret.intValue();
        else
            return null;
    }

    /*
     * return the max integer value of a divide b
     * 
     * @para int a, int b
     * 
     * @return Long as ceil(a/b)
     */
    public static Long divideAndCeil(long a, long b) {
        Double ret = null;
        double aa = a;
        double bb = b;
        if (bb != 0)
            ret = Math.ceil(aa / bb);
        if (ret != null)
            return ret.longValue();
        else
            return null;
    }

    /*
     * return the max integer value of a divide b
     * 
     * @para long a, int b
     * 
     * @return Long as ceil(a/b)
     */
    public static Long divideAndCeil(long a, int b) {
        Double ret = null;
        double aa = a;
        double bb = b;
        if (bb != 0)
            ret = Math.ceil(aa / bb);
        if (ret != null)
            return ret.longValue();
        else
            return null;
    }

    public static Integer divideToCeil(long a, long b) {
        Double ret = null;
        double aa = a;
        double bb = b;
        if (bb != 0)
            ret = Math.ceil(aa / bb);
        if (ret != null)
            return ret.intValue();
        else
            return null;
    }

    public static Long divideAndFloor(long a, long b) {
        Double ret = null;
        double aa = a;
        double bb = b;
        if (bb != 0)
            ret = Math.floor(aa / bb);
        if (ret != null)
            return ret.longValue();
        else
            return null;
    }

    /*
     * return the max integer value of a divide b
     * 
     * @para long a, long b
     * 
     * @return Long as floor(a/b)
     */
    public static Integer divideToFloor(long a, long b) {
        Double ret = null;
        double aa = a;
        double bb = b;
        if (bb != 0)
            ret = Math.floor(aa / bb);
        if (ret != null)
            return ret.intValue();
        else
            return null;
    }

    public static List<String> getNifixExp(String strExp) {

        int nLen = strExp.length();
        LinkedList<String> listExp = new LinkedList<String>();
        for (int i = 0; i < nLen; i++) {
            char ch = strExp.charAt(i);
            String str = listExp.isEmpty() ? null : listExp.getLast();

            if (CharUtils.isAsciiNumeric(ch)) {
                if (null == str || !StringUtils.isNumeric(str)) {
                    listExp.add("" + ch);
                } else {
                    listExp.set(listExp.size() - 1, str + ch);
                }
            } else {
                listExp.add("" + ch);
            }

        }
        return listExp;
    }
}
